package top.hitotright.es;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.ParsedAvg;
import org.elasticsearch.search.aggregations.metrics.ParsedMax;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.Map;

public class Test {

    private RestHighLevelClient client;

    private SearchResponse  response;

    public Test (){
        client = new RestHighLevelClient(
                RestClient
                        .builder(
                                new HttpHost("172.16.100.16", 9200, "http")
                        ));
    }

    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void deleteIndex(){
        DeleteIndexRequest request = new DeleteIndexRequest("dev_review_book_es");
        try {
            client.indices().delete(request,RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println("test start ...");
        Test app = new Test();
        app.search();
        app.close();
    }

    public void search(){
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        sourceBuilder.query(QueryBuilders.termQuery("realName", "专家1"));
        TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_cId")
                .field("cId");
        aggregation.subAggregation(AggregationBuilders.avg("avg_reviewCircle")
                .field("reviewCircle"));
        aggregation.subAggregation(AggregationBuilders.max("max_score")
                .field("score"));

        sourceBuilder.aggregation(aggregation);
        SearchRequest searchRequest = new SearchRequest("dev_review_book_es");
        searchRequest.source(sourceBuilder);
        System.out.println("dsl is："+sourceBuilder);
        try {
            response = client.search(searchRequest,RequestOptions.DEFAULT);
            System.out.println("status:"+ response.status());
        } catch (IOException e) {
            e.printStackTrace();
        }

        Terms terms = response.getAggregations().get("by_cId");
        for (Terms.Bucket b : terms.getBuckets()) {
            Map subAgg =  b.getAggregations().asMap();
            double avg_reviewCircle = ((ParsedAvg) subAgg.get("avg_reviewCircle")).getValue();
            double max_score = ((ParsedMax) subAgg.get("max_score")).getValue();
            System.out.println("专家编号:" +b.getKey() + ",评审数量:" + b.getDocCount() +",最高分:"+max_score+"平均周期(天):"+avg_reviewCircle);
        }

    }

    public void bulk(){
//        ReviewBookES rb1 = new ReviewBookES();
//        rb1.setRbId(1L);
//        rb1.setTsId(1L);
//        rb1.setTsName("论文1");
//        rb1.setDeptId(1L);
//        rb1.setDeptName("南京大学");
//        rb1.setcId(1L);
//        rb1.setRealName("专家1");
//        rb1.setPhone("111111");
//        rb1.setCreateTime(new Date());
//        rb1.setOverTime(new Date());
//        rb1.setRrId(1L);
//        rb1.setRtId(1L);
//        rb1.setReviewTime(new Date());
//        rb1.setReviewCircle(1);
//        rb1.setContentLength(100);
//        rb1.setScore(90.0f);
//        rb1.setDefense(true);
//        rb1.setExcellent(true);
//        rb1.setDeleteFleg(false);
//
//        ReviewBookES rb2 = new ReviewBookES();
//        rb2.setRbId(2L);
//        rb2.setTsId(2L);
//        rb2.setTsName("论文2");
//        rb2.setDeptId(1L);
//        rb2.setDeptName("南京大学");
//        rb2.setcId(2L);
//        rb2.setRealName("专家2");
//        rb2.setPhone("222222");
//        rb2.setCreateTime(new Date());
//        rb2.setOverTime(new Date());
//        rb2.setRrId(1L);
//        rb2.setRtId(1L);
//        rb2.setReviewTime(new Date());
//        rb2.setReviewCircle(2);
//        rb2.setContentLength(200);
//        rb2.setScore(80.0f);
//        rb2.setDefense(true);
//        rb2.setExcellent(false);
//        rb2.setDeleteFleg(false);
//
//        ReviewBookES rb3 = new ReviewBookES();
//        rb3.setRbId(3L);
//        rb3.setTsId(3L);
//        rb3.setTsName("论文3");
//        rb3.setDeptId(1L);
//        rb3.setDeptName("南京大学");
//        rb3.setcId(1L);
//        rb3.setRealName("专家1");
//        rb3.setPhone("111111");
//        rb3.setCreateTime(new Date());
//        rb3.setOverTime(new Date());
//        rb3.setRrId(1L);
//        rb3.setRtId(1L);
//        rb3.setReviewTime(new Date());
//        rb3.setReviewCircle(2);
//        rb3.setContentLength(150);
//        rb3.setScore(80.0f);
//        rb3.setDefense(false);
//        rb3.setExcellent(true);
//        rb3.setDeleteFleg(false);

        //批量处理数据
        BulkRequest request = new BulkRequest();
//        request.add(new IndexRequest("dev_review_book_es","review_book",rb1.getRbId().toString())
//                .source(JSON.toJSON(rb1).toString(), XContentType.JSON));
//        request.add(new IndexRequest("dev_review_book_es","review_book",rb2.getRbId().toString())
//                .source(JSON.toJSON(rb2).toString(), XContentType.JSON));
//        request.add(new IndexRequest("dev_review_book_es","review_book",rb3.getRbId().toString())
//                .source(JSON.toJSON(rb3).toString(), XContentType.JSON));

        try {
            BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
            System.out.println(responses);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
